package com.sojson.project.controller;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.controller.param.verify.AreaInfoParamVerify;
import com.sojson.project.entity.po.AreaInfo;
import com.sojson.project.service.AreaInfoService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 省市区信息表控制器
 * 
 * @author liu
 * @date 2021-04-25
 */
@Api(tags = "省市区信息表管理")
@RestController
@RequestMapping("/areaInfo")
public class AreaInfoController {

    @Autowired
    private AreaInfoService areaInfoService;

    /**
     * 单条插入
     * 
     * @param areaInfo
     * @return
     * @throws SQLException 
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/areaInfo/insert')")
    @ApiOperation("添加省市区")
    public Object insert(@RequestBody AreaInfo areaInfo) throws SQLException {
        // 校验参数
        Result<?> paramResult = AreaInfoParamVerify.insert(areaInfo);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(areaInfoService.insert(areaInfo));
    }

    /**
     * 批量插入
     * 
     * @param areaInfo
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/areaInfo/insertList')")
    @ApiOperation("批量添加省市区")
    public Object insertList(@RequestBody AreaInfo areaInfo) throws SQLException {
        return Result.success(areaInfoService.insertList(areaInfo));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param areaInfo
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/areaInfo/deleteByDto')")
    @ApiOperation("通过对象批量删除省市区(物理删除)")
    public Object deleteByDto(@RequestBody AreaInfo areaInfo) throws SQLException {
        return Result.success(areaInfoService.deleteByDto(areaInfo));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    // @GetMapping("/countAll")
    public Object countAll() throws SQLException {
        return Result.success(areaInfoService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    // @GetMapping("/findAll")
    public Object findAll() throws SQLException {
        return Result.success(areaInfoService.findAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param areaInfo
     * @return
     */
    // @GetMapping("/countByDto")
    public Object countByDto(AreaInfo areaInfo) throws SQLException {
        return Result.success(areaInfoService.countByDto(areaInfo));
    }

    /**
     * 根据对象查询
     * 
     * @param areaInfo
     * @return
     */
    @GetMapping("/findByDto")
    @ApiOperation(value = "根据对象查询省市区", response = AreaInfo.class)
    public Object findByDto(AreaInfo areaInfo) throws SQLException {
        return Result.success(areaInfoService.findByDto(areaInfo));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    @PreAuthorize("@ss.hasPermi('/areaInfo/deleteById')")
    @ApiOperation("通过id删除省市区(物理删除)")
    public Object deleteById(Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(areaInfoService.deleteById(id));
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIdList")
    @PreAuthorize("@ss.hasPermi('/areaInfo/deleteByIdList')")
    @ApiOperation("通过id列表删除省市区(物理删除)")
    public Object deleteByIdList(@RequestParam("ids") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(areaInfoService.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param areaInfo
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/areaInfo/updateById')")
    @ApiOperation("通过id更新省市区(全部更新)")
    public Object updateById(@RequestBody AreaInfo areaInfo) {
        return Result.success(areaInfoService.updateById(areaInfo));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param areaInfo
     * @return
     */
    @PutMapping("/updateStaticByIdList")
    @PreAuthorize("@ss.hasPermi('/areaInfo/updateStaticByIdList')")
    @ApiOperation("通过id更新省市区(全部更新)(更新内容相同)")
    public Object updateStaticByIdList(@RequestBody AreaInfo areaInfo) {
        return Result.success(areaInfoService.updateStaticByIdList(areaInfo));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param areaInfo
     * @return
     */
    @PutMapping("/updateChangeByIdList")
    @PreAuthorize("@ss.hasPermi('/areaInfo/updateChangeByIdList')")
    @ApiOperation("通过id更新省市区(全部更新)(更新内容不相同)")
    public Object updateChangeByIdList(@RequestBody AreaInfo areaInfo) {
        return Result.success(areaInfoService.updateChangeByIdList(areaInfo));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param areaInfo
     * @return
     */
    @PutMapping("/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/areaInfo/updatePartialById')")
    @ApiOperation("通过id更新省市区(局部更新)")
    public Object updatePartialById(@RequestBody AreaInfo areaInfo) {
        // 校验参数
        Result<?> paramResult = AreaInfoParamVerify.updatePartialById(areaInfo);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(areaInfoService.updatePartialById(areaInfo));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param areaInfo
     * @return
     */
    @PutMapping("/updateStaticPartialByIdList")
    @PreAuthorize("@ss.hasPermi('/areaInfo/updateStaticPartialByIdList')")
    @ApiOperation("通过id列表更新省市区(局部更新)(更新内容相同)")
    public Object updateStaticPartialByIdList(@RequestBody AreaInfo areaInfo) {
        return Result.success(areaInfoService.updateStaticPartialByIdList(areaInfo));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param areaInfo
     * @return
     */
    @PutMapping("/updateChangePartialByIdList")
    @PreAuthorize("@ss.hasPermi('/areaInfo/updateChangePartialByIdList')")
    @ApiOperation("通过id列表更新省市区(局部更新)(更新内容不相同)")
    public Object updateChangePartialByIdList(@RequestBody AreaInfo areaInfo) {
        return Result.success(areaInfoService.updateChangePartialByIdList(areaInfo));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation(value = "通过id查询省市区", response = AreaInfo.class)
    public Object findById(Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(areaInfoService.findById(id));
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(areaInfoService.findByIdList(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param areaInfo
     * @return
     */
    // @GetMapping("/findIdByDto")
    public Object findIdByDto(AreaInfo areaInfo) {
        return Result.success(areaInfoService.findIdByDto(areaInfo));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/areaInfo/clear')")
    @ApiOperation("清空省市区(物理清空)")
    public Object clear() {
        areaInfoService.clear();
        return Result.success();
    }

}